#ifndef __xwt_opengl_view_h
  #define __xwt_opengl_view_h

#include "xwt_view.h"

XWT_BEGIN_NAMESPACE(xwt)

/* A view supporting open gl rendering */
class OpenGLView : public View {
public:
	/* constructors */

	// default construction
	OpenGLView ();

	// dtor
	~OpenGLView ();

public:
	/**
	 * SetupGLView ()
	 * Setups the OpenGL view port and matrices.
	 */
	virtual void SetupGLView (int cx, int cy);

	/**
	 * SetPerspectiveViewingMode ()
	 * Sets the viewing mode to perspective.
	 */
	void SetPerspectiveViewingMode ();

	/**
	 * SetOrthographicViewingMode ()
	 * Sets the viewing mode to orthographic mode.
	 */
	void SetOrthographicViewingMode ();

	bool GetOrtho () const {
		return m_ortho;
	}
	
	void SetOrtho (bool flag) {
		m_ortho = flag;
	}
	
	/**
	 * RenderScene ()
	 * Renders the scene. The default implementation will erase the screen with 
	 * current clear color. Override to render / draw the models.
	 */
	virtual void RenderScene ();	

protected:
	/**
	 * SetupPixelFormat ()
	 * Setups the pixel format that suuports OpenGL.
	 */
	bool SetupPixelFormat ();

	/**
	 * CreateGLContext ()
	 * Creates OpenGL context to be used with this view.
	 */
	bool CreateGLContext ();

	/**
	 * DestroyGLContext ()
	 * Destroys the OpenGL context.
	 */
	void DestroyGLContext ();

	/**
	 * InitGL ()
	 * Initializes the defaults for OpenGL.
	 */
	virtual void InitGL ();

	// overrides...
#ifdef WIN32
protected:
	virtual void GetCreateStyle (DWORD &dwStyle, DWORD &dwExStyle);
#endif

public:
	void ResetView ();
	void SetViewPort (GLint vpx, GLint vpy, GLsizei vpcx, GLsizei vpcy);

protected:
	// message handlers
	XWT_DECL_EVENT_HANDLER(Create) ();
	XWT_DECL_EVENT_HANDLER(PreDestroy) ();
	XWT_DECL_EVENT_HANDLER(EraseBackground) (GC &gc);
	XWT_DECL_EVENT_HANDLER(Resize) (int cx, int cy);

protected:
	// data members
#ifdef WIN32
	HGLRC m_hGLRC;	
#endif
	GLdouble m_orthographicMatrix[16];
	GLdouble m_perspectiveMatrix[16];
	GLint m_vpLeft;
	GLint m_vpTop;
	GLsizei m_vpWidth;
	GLsizei m_vpHeight;
	bool m_ortho;
};

XWT_END_NAMESPACE(xwt);

#endif /* __xwt_opengl_view_h */
